package com.item.news.runnable;

import com.alibaba.fastjson.JSON;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName Top_newsRunnable
 * @Description TODO
 * @Author WangDong
 * @Date 2019/1/21 11:23
 * @Version 1.0
 **/
public class Other_newsRunnable implements Runnable {

    private Jedis jedis;

    private String url;

    private String name;

    public Other_newsRunnable(Jedis jedis, String url, String name) {
        this.jedis = jedis;
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        news_ohter();
    }
    public void news_ohter(){

        List<HashMap<String,String>> data=new ArrayList<HashMap<String,String>>();
        try {
            //构造一个webClient 模拟Chrome 浏览器
            WebClient webClient = new WebClient(BrowserVersion.CHROME);

            //支持JavaScript
            webClient.getOptions().setJavaScriptEnabled(true);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setActiveXNative(false);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setTimeout(10000);
            HtmlPage rootPage = webClient.getPage(url);
            //设置一个运行JavaScript的时间
            webClient.waitForBackgroundJavaScript(10000);
            String html = rootPage.asXml();
            Document doc = Jsoup.parse(html);
            /*  System.out.println(doc);*/
            Elements img=doc.select("div.data_row > a >img");
            Elements title=doc.select("div.news_title > h3 >a");
            Elements tag=doc.select("div.news_tag > div.keywords ");
            Elements time=doc.select("div.news_tag > span.time");

            for(int i=0;i<title.size();i++){
                HashMap<String,String>maps=new HashMap<String,String>();
                title.eq(i).select("a").text();
                img.eq(i).select("img").attr("src");
                img.eq(i).select("img").attr("href");
                tag.eq(i).select("div").select("a").text();
                maps.put("title", title.eq(i).select("a").text());
                maps.put("img",img.eq(i).select("img").attr("src"));
                maps.put("href",title.eq(i).select("a").attr("href"));
                maps.put("tag",tag.eq(i).select("div").select("a").text());
                maps.put("time",time.eq(i).select("span").text());
                data.add(maps);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!data.isEmpty()){
            jedis.set(name, JSON.toJSONString(data));
        }

    }
}
